/**
 * Resource.java
 *
 * This Class contains all resources such as Image and Audio resources
 * that are required by a Sprite.
 *
 * @author Somanath
 * @version 1.1 (last updated 20/6/07)
 *
 * class Dependencies: None
 */

 package gamepack;

import java.util.*;
/**
                                 ---> [Image Resources]
                                |
 *            [SpriteResource]--|---> [General Resources]
 *                              |
 *                               ---> [AudioResources]
 *
 *   A simple structure contains all Sprite resources
 *   like image, sound and music.
 */

public class Resource {

    // The name of the resource, typically the name of the entity that
    // uses this resource.
    public String name;

    // 2D array is used as it provides lot flexibility while using images.
    // It allows to logically group a set of images together under 1D array
	// Eg. - img[RUNNING][0], img[RUNNING[1], img[STANDING][0], img[STANDING][1]
    public CImage[][] img;

    // Sound and Music data
    public Sound[] sound;
    public Music[] music;

    // Other data
    public Map<String,String> data;
	public PreferredSpawnString[] pss; // For rooms to know which sprites to spawn
	public Map<String,RoomData> roomData;

    public int getValueAsInt(String attribName) {
       String val = data.get(attribName);
       if( val == null || val.equals("") ) {
           throw new RuntimeException("Attribute " + attribName + " not found");
       }
       return Integer.parseInt( val.trim() );
    }
	 public String getValue(String attribName) {
       String val = data.get(attribName);
       if( val == null || val.equals("") ) {
           throw new RuntimeException("Attribute " + attribName + " not found");
       }
       return val.trim();
    }
	
	// Promise - Returns an array of 8 Integers. First 4 represent Viewport x,y,width,height, Next 4 represent innerbox left,right,up,down.
	public int[] getViewportDetails() { 
	   String val = data.get("viewport");
       if( val == null || val.equals("") ) {
           throw new RuntimeException("Attribute 'viewport' not found");
       }
       val = val.trim();
	   String[] parts = val.split(",");

       if( parts.length < 8 ) {
           throw new RuntimeException("<Data> name='viewport' Required 8 parameters, but found only ("+parts.length+") </Data>");
       }
	   int[] retArr = new int[8];
	   for(int i=0;i<8;i++) {
		retArr[i] = Integer.parseInt( parts[i].trim() );
	   }
	   return retArr;
	}

    public String getData(String attribName) {
       String val = data.get(attribName);
       if( val == null || val.equals("") ) {
           throw new RuntimeException("Attribute " + attribName + " not found");
       }
       return val.trim();
    }
}